package com.hkts.management.pay.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.management.common.utils.Result;
import com.hkts.management.feign.FeignInterface;
import com.hkts.management.feign.FrontInterface;
import com.hkts.management.model.TO.HontUserTo;
import com.hkts.management.pay.entity.HktsPayRecord;
import com.hkts.management.pay.mapper.HktsPayRecordMapper;
import com.hkts.management.pay.service.IHktsPayRecordService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-08-12
 */
@Service
@Transactional
public class HktsPayRecordServiceImpl extends ServiceImpl<HktsPayRecordMapper, HktsPayRecord> implements IHktsPayRecordService {
    @Autowired
    private FrontInterface frontInterface;
    @Autowired
    private HktsPayRecordMapper hktsPayRecordMapper;
    @Autowired
    private FeignInterface feignInterface;

    @Override
    public IPage<HktsPayRecord> selectPage(Page<HktsPayRecord> page, HktsPayRecord hktsPayRecord) {
        QueryWrapper<HktsPayRecord> query = Wrappers.query();
        QueryWrapper<HktsPayRecord> entityWrapper = getEntityWrapper(query, hktsPayRecord);
        if(entityWrapper==null){
            return   page;
        }
        entityWrapper.lambda().orderByDesc(HktsPayRecord::getCreateTime);
        Page<HktsPayRecord> hktsPayRecordPage = hktsPayRecordMapper.selectPage(page, entityWrapper);
        if(hktsPayRecordPage.getRecords().size()>0){
            StringBuffer buffer = new StringBuffer();
            for (int i = 0; i < hktsPayRecordPage.getRecords().size(); i++) {
                buffer.append(hktsPayRecordPage.getRecords().get(i).getDriverId()).append(",");
            }
            Result<Map<String, String>> driverNamesByIdsBy = frontInterface.getDriverNamesByIdsBy(buffer.toString());
            for (int i = 0; i < hktsPayRecordPage.getRecords().size(); i++) {
                hktsPayRecordPage.getRecords().get(i).setDriverName(driverNamesByIdsBy.getResult().get(hktsPayRecordPage.getRecords().get(i).getDriverId()));
            }
        }

        return hktsPayRecordMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HktsPayRecord> selectList(HktsPayRecord hktsPayRecord) {
        QueryWrapper<HktsPayRecord> query = Wrappers.query();
        QueryWrapper<HktsPayRecord> entityWrapper = getEntityWrapper(query, hktsPayRecord);
        return hktsPayRecordMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HktsPayRecord> getEntityWrapper(QueryWrapper<HktsPayRecord> entityWrapper, HktsPayRecord hktsPayRecord) {
        HontUserTo hontUserTo = feignInterface.remoteUserInfo();
        //条件拼接
        if (StringUtils.isNotBlank(hktsPayRecord.getPayOrder())) {
            entityWrapper.eq(HktsPayRecord.PAY_ORDER, hktsPayRecord.getPayOrder());
        }
        if (StringUtils.isNotBlank(hktsPayRecord.getDriverName())) {
            Result<List<String>> idsByDriverName = frontInterface.getIdsByDriverName(hktsPayRecord.getDriverName());
            if(idsByDriverName.getResult().size()==0){
                return null;
            }
            entityWrapper.lambda().in(HktsPayRecord::getDriverId, idsByDriverName.getResult());
        }
        if (StringUtils.isNotBlank(hktsPayRecord.getDriverId())) {
            entityWrapper.like(HktsPayRecord.DRIVER_ID, hktsPayRecord.getDriverId());
        }
        if (StringUtils.isNotBlank(hktsPayRecord.getOrderId())) {
            entityWrapper.like(HktsPayRecord.ORDER_ID, hktsPayRecord.getOrderId());
        }
        if (StringUtils.isNotBlank(hktsPayRecord.getPayStatus())) {
            entityWrapper.like(HktsPayRecord.PAY_STATUS, hktsPayRecord.getPayStatus());
        }
        if (StringUtils.isNotBlank(hktsPayRecord.getCompanyCode())) {
            entityWrapper.like(HktsPayRecord.COMPANY_CODE, hktsPayRecord.getCompanyCode());
        }
        if (StringUtils.isNotBlank(hktsPayRecord.getStartTime())) {
            entityWrapper.ge(HktsPayRecord.CREATE_TIME, hktsPayRecord.getStartTime());
        }
        if (StringUtils.isNotBlank(hktsPayRecord.getEndTime())) {
            entityWrapper.le(HktsPayRecord.CREATE_TIME, hktsPayRecord.getEndTime());
        }
        if (StringUtils.isNotBlank(hontUserTo.getCompanyId())) {
            entityWrapper.lambda().apply(HktsPayRecord.COMPANY_CODE + " like '" + hontUserTo.getCompanyId() + "%'");
        }
        return entityWrapper;
    }
}
